﻿using MediatR;
using RedDragon.Common.Domain.InventoryAllocate;
using RedDragon.Common.Infrastructure.Interface;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.Inventoryscheduling
{
    public class InventoryDetailSearchHandler : IRequestHandler<InventoryDetailSearchCommon, Result>
    {
        public readonly IBaseRepository<InventoryDetail> InventoryDetailRepository;
        public readonly IBaseRepository<Inventory> InventoryRepository;

        public InventoryDetailSearchHandler(IBaseRepository<InventoryDetail> inventoryDetailRepository, IBaseRepository<Inventory> inventoryRepository)
        {
            InventoryDetailRepository = inventoryDetailRepository;
            InventoryRepository = inventoryRepository;
        }

        public async Task<Result> Handle(InventoryDetailSearchCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = 0, Msg = "" };
            try
            {

                var list = from a in InventoryDetailRepository.Query()
                           select new InventoryDetailDto()
                           {
                               InventoryDetailId=a.InventoryDetailId,
                               AllocateNumber = a.AllocateNumber,
                               MaterialId= a.MaterialId,
                               MaterialName = a.MaterialName,
                               SpecificationModel = a.SpecificationModel,
                               Unit = a.Unit,
                               AllocateCount = a.AllocateCount,
                               InventoryCount = a.InventoryCount,
                               Summary = a.Summary
                           };
                if (request.AllocateNumber == null)
                {
                    InventoryDetailRepository.Query().Where(x => x.AllocateNumber == request.AllocateNumber);
                }



                result.Data = list;

            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
    public class InventoryDetailDto
    {
        public int InventoryDetailId { get; set; }
        /// <summary>
        /// 调拨编码
        /// </summary>
        public string? AllocateNumber { get; set; }
        /// <summary>
        /// 物料编码
        /// </summary>
        public int MaterialId { get; set; }
        /// <summary>
        /// 物料名称
        /// </summary>
        public string? MaterialName { get; set; }
        /// <summary>
        /// 规格型号
        /// </summary>
        public string? SpecificationModel { get; set; }
        /// <summary>
        /// 单位
        /// </summary>
        public string? Unit { get; set; }
        /// <summary>
        /// 调拨数量
        /// </summary>
        public int AllocateCount { get; set; }
        /// <summary>
        /// 库存数量
        /// </summary>
        public int InventoryCount { get; set; }
        /// <summary>
        /// 摘要
        /// </summary>
        public string? Summary { get; set; }
    }
}
